package com.camel.iamfx;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class FxUtil {

    public static final Logger logger = LoggerFactory.getLogger(FxUtil.class);

    /**
     * 加载JAR包到本地系统
     *
     * @param jarFile
     */
    public static void addJarClass(File jarFile) {
        try {
            logger.info("读取Lib包文件: " + jarFile.getName());
            Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
            method.setAccessible(true);
            URL url = jarFile.toURI().toURL();
            URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
            method.invoke(urlClassLoader, url);
        } catch (Exception e) {
            e.printStackTrace();
            LoggerFactory.getLogger(FxUtil.class).error("添加libs中jar包到系统中异常:", e);
        }
    }
}
